<?php
class C_dokumen extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_dokumen');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			default :
				$data["data_t_proyek_id"] = $this->m_dokumen->getDataProyek();
				$data["data_dokumen_asli"] = array("","Asli","Copy");
				$this->load->view('v_dokumen',$data);
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['dokumen_nama'] = nosql_post("dokumen_nama");
		$data['dokumen_url'] = nosql_post("dokumen_url");
		$data['dokumen_asli'] = nosql_post("dokumen_asli");
		$data['dokumen_posisi'] = nosql_post("dokumen_posisi");
		$data['t_proyek_id'] = nosql_post("t_proyek_id");
		echo $this->m_dokumen->_list($data);
	}
	
	function _insert(){
		
		$config['upload_path'] = './upload/';
		$config['allowed_types'] = '*';
		$config['file_name'] = $this->input->post('dokumen_nama')."-".date('ymdhis');
		$config['max_size']	= '200000';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = $this->upload->display_errors();
			echo '{"success":"true","msg":"'.$error.'"}';	
		}
		$uploaddata = $this->upload->data();
		$data = array(
			'dokumen_nama' => $this->input->post('dokumen_nama'), 
			'dokumen_url' => "upload/".$uploaddata["file_name"], 
			'dokumen_asli' => $this->input->post('dokumen_asli'), 
			'dokumen_posisi' => $this->input->post('dokumen_posisi'), 
			't_proyek_id' => $this->input->post('t_proyek_id'), 
			  
			'dokumen_sys_active'=> '1',
			'dokumen_sys_owner'	=>$_SESSION[SESSION_USERID]);
		$this->m_dokumen->_insert($data);
		
		echo 1;
	}
	
	function _update(){
	
		$dokumen_id = $this->input->post('dokumen_id');
		$data = array(
			'dokumen_nama' => $this->input->post('dokumen_nama'), 
			'dokumen_asli' => $this->input->post('dokumen_asli'), 
			'dokumen_posisi' => $this->input->post('dokumen_posisi'), 
			't_proyek_id' => $this->input->post('t_proyek_id'), 
			
			'dokumen_sys_owner'	=> $_SESSION[SESSION_USERID],
			'dokumen_sys_update'=> date("Y-m-d h:i:s"));
			
		$config['upload_path'] = './upload/';
		$config['allowed_types'] = '*';
		$config['file_name'] = $this->input->post('dokumen_nama')."-".date('ymdhis');
		$config['max_size']	= '200000';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = $this->upload->display_errors();
			echo '{"success":"true","msg":"'.$error.'"}';	
		}else{
		
			$uploaddata = $this->upload->data();
			$data["dokumen_url"] = "upload/".$uploaddata["file_name"];
		}
		$this->m_dokumen->_update($data,$dokumen_id);
		echo 1;
 	}
	
	function _delete(){
		$dokumen_id= $this->input->post('dokumen_id');
		$data = array(
			'dokumen_sys_active' => '0',
			'dokumen_sys_owner'	=> $_SESSION[SESSION_USERID],
			'dokumen_sys_delete'=> date("Y-m-d h:i:s")
		);
		$this->m_dokumen->_delete($data,$dokumen_id);
		echo 1;
	}
}